// UpdateCallbackGUI.cpp

#include "StdAfx.h"

#include "Common/String/StringConvert.h"

#include "Common/System/Error.h"

#include "../FileManager/PasswordDialog.h"

#include "UpdateCallbackGUI.h"

#include "Common/Defs.h"

using namespace Common::System;

CUpdateCallbackGUI::~CUpdateCallbackGUI() {}

void CUpdateCallbackGUI::Init()
{
  FailedFiles.clear();
  NumFiles = 0;
}

void CUpdateCallbackGUI::AddErrorMessage(LPCTSTR message)
{
  ProgressDialog->Sync.AddErrorMessage(message);
}

void CUpdateCallbackGUI::AddErrorMessage(const TCHAR *name, DWORD systemError)
{
  AddErrorMessage(
      CString(_T("WARNING: ")) +
      Error::FormatMessage(systemError) +
      CString(_T(": ")) +
      CString(name));
}

HRESULT CUpdateCallbackGUI::OpenResult(const TCHAR *name, HRESULT result)
{
  if (result != S_OK)
  {
    AddErrorMessage (CString(_T("Error: ")) + name + CString(_T(" is not supported archive")));
  }
  return S_OK;
}

HRESULT CUpdateCallbackGUI::StartScanning()
{
  return S_OK;
}

HRESULT CUpdateCallbackGUI::CanNotFindError(const TCHAR *name, DWORD systemError)
{
	FailedFiles.push_back(name);
  AddErrorMessage(name, systemError);
  return S_OK;
}

HRESULT CUpdateCallbackGUI::FinishScanning()
{
  return S_OK;
}

HRESULT CUpdateCallbackGUI::StartArchive(const TCHAR *name, bool /* updating */)
{
  ProgressDialog->Sync.SetTitleFileName(name);
  return S_OK;
}

HRESULT CUpdateCallbackGUI::FinishArchive()
{
  return S_OK;
}

HRESULT CUpdateCallbackGUI::CheckBreak()
{
  return ProgressDialog->Sync.ProcessStopAndPause();
}

HRESULT CUpdateCallbackGUI::ScanProgress(UINT64 /* numFolders */, UINT64 numFiles, const wchar_t *path)
{
  ProgressDialog->Sync.SetCurrentFileName(path);
  ProgressDialog->Sync.SetNumFilesTotal(numFiles);
  return ProgressDialog->Sync.ProcessStopAndPause();
}

STDMETHODIMP CUpdateCallbackGUI::Finilize()
{
  return S_OK;
}

HRESULT CUpdateCallbackGUI::SetNumFiles(UINT64 numFiles)
{
  ProgressDialog->Sync.SetNumFilesTotal(numFiles);
  return S_OK;
}

STDMETHODIMP CUpdateCallbackGUI::SetTotal(UINT64 total)
{
  ProgressDialog->Sync.SetProgress(total, 0);
  return S_OK;
}

HRESULT CUpdateCallbackGUI::SetCompleted(const UINT64 *completeValue)
{
  HRESULT hRes = CheckBreak();
  if(S_OK != hRes)
	return hRes;
  if (completeValue != NULL)
    ProgressDialog->Sync.SetPos(*completeValue);
  return S_OK;
}

STDMETHODIMP CUpdateCallbackGUI::SetRatioInfo(const UINT64 *inSize, const UINT64 *outSize)
{
  HRESULT hRes = CheckBreak();
  if(S_OK != hRes)
	return hRes;
  ProgressDialog->Sync.SetRatioInfo(inSize, outSize);
  return S_OK;
}

STDMETHODIMP CUpdateCallbackGUI::GetStream(const TCHAR *name, bool /* isAnti */)
{
  ProgressDialog->Sync.SetCurrentFileName(name);
  return S_OK;
}

STDMETHODIMP CUpdateCallbackGUI::OpenFileError(const TCHAR *name, DWORD systemError)
{
	FailedFiles.push_back(name);
  // if (systemError == ERROR_SHARING_VIOLATION)
  {
    AddErrorMessage(name, systemError);
    return S_FALSE;
  }
  // return systemError;
}

STDMETHODIMP CUpdateCallbackGUI::SetOperationResult(INT32 /* operationResult */)
{
  NumFiles++;
  ProgressDialog->Sync.SetNumFilesCur(NumFiles);
  return S_OK;
}

STDMETHODIMP CUpdateCallbackGUI::CryptoGetTextPassword2(INT32 *passwordIsDefined, BSTR *password)
{
  *password = NULL;
  if (!PasswordIsDefined)
  {
    if (passwordIsDefined == 0 || AskPassword)
    {
      CPasswordDialog dialog;
      ProgressDialog->WaitCreating();
      //if (dialog.Create(*ProgressDialog) == IDCANCEL)
			if(IDCANCEL == dialog.DoModal(*ProgressDialog))
        return E_ABORT;
      Password = dialog.Password;
      PasswordIsDefined = true;
    }
  }
  if (passwordIsDefined != 0)
    *passwordIsDefined = BoolToInt(PasswordIsDefined);
  return StringToBstr(Password, password);
}

STDMETHODIMP CUpdateCallbackGUI::CryptoGetTextPassword(BSTR *password)
{
  return CryptoGetTextPassword2(NULL, password);
}

/*
It doesn't work, since main stream waits Dialog
HRESULT CUpdateCallbackGUI::CloseProgress()
{
  ProgressDialog->MyClose();
  return S_OK;
}
*/


STDMETHODIMP CUpdateCallbackGUI::Open_CheckBreak()
{
  return ProgressDialog->Sync.ProcessStopAndPause();
}

STDMETHODIMP CUpdateCallbackGUI::Open_SetTotal(const UINT64 * /* numFiles */, const UINT64 * /* numBytes */)
{
  // if (numFiles != NULL) ProgressDialog->Sync.SetNumFilesTotal(*numFiles);
  return S_OK;
}

HRESULT CUpdateCallbackGUI::Open_SetCompleted(const UINT64 * /* numFiles */, const UINT64 * /* numBytes */)
{
  return ProgressDialog->Sync.ProcessStopAndPause();
}

#ifndef _NO_CRYPTO

HRESULT CUpdateCallbackGUI::Open_CryptoGetTextPassword(BSTR *password)
{
  PasswordWasAsked = true;
  return CryptoGetTextPassword2(NULL, password);
}

HRESULT CUpdateCallbackGUI::Open_GetPasswordIfAny(CString &password)
{
  if (PasswordIsDefined)
    password = Password;
  return S_OK;
}

bool CUpdateCallbackGUI::Open_WasPasswordAsked()
{
  return PasswordWasAsked;
}

void CUpdateCallbackGUI::Open_ClearPasswordWasAskedFlag()
{
  PasswordWasAsked = false;
}

/*
HRESULT CUpdateCallbackGUI::ShowDeleteFile(const wchar_t *name)
{
  ProgressDialog->Sync.SetCurrentFileName(name);
  return S_OK;
}
*/

#endif
